package com.nnnu.wsnackshop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nnnu.wsnackshop.mapper.ProductTagsMapper;
import com.nnnu.wsnackshop.mapper.TagsMapper;
import com.nnnu.wsnackshop.pojo.dto.AssignProductTagsDTO;
import com.nnnu.wsnackshop.pojo.dto.RemoveProductTagsDTO;
import com.nnnu.wsnackshop.pojo.entity.ProductTags;
import com.nnnu.wsnackshop.pojo.entity.Tags;
import com.nnnu.wsnackshop.pojo.vo.TagVO;
import com.nnnu.wsnackshop.service.IProductTagsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 商品-标签关联 服务实现类
 * </p>
 *
 * @author zk
 * @since 2025-05-14
 */
@Service
@RequiredArgsConstructor
public class ProductTagsServiceImpl extends ServiceImpl<ProductTagsMapper, ProductTags> implements IProductTagsService {

    private final TagsMapper tagsMapper;

    @Override
    public List<TagVO> listTagsByProduct(Long productId) {
        // 查询中间表得到 tagId 列表
        List<Integer> tagIds = baseMapper.selectList(
                Wrappers.<ProductTags>lambdaQuery()
                        .eq(ProductTags::getProductId, productId))
                .stream().map(ProductTags::getTagId).toList();

        if (tagIds.isEmpty()) {
            return Collections.emptyList();
        }
        // 查询标签详情并转换为 VO
        List<Tags> tags = tagsMapper.selectBatchIds(tagIds);
        return tags.stream().map(tag -> {
            TagVO vo = new TagVO();
            BeanUtil.copyProperties(tag, vo);
            return vo;
        }).toList();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void assignTags(AssignProductTagsDTO dto) {
        Long productId = dto.getProductId();
        List<Long> newTagIds = dto.getTagIds();

        // 先删除该商品所有已有关联
        baseMapper.delete(
                Wrappers.<ProductTags>lambdaQuery()
                        .eq(ProductTags::getProductId, productId));
        // 再批量插入新的关联
        List<ProductTags> entities = newTagIds.stream()
                .map(tagId -> new ProductTags()
                        .setProductId(Math.toIntExact(productId))
                        .setTagId(Math.toIntExact(tagId)))
                .toList();
        if (!entities.isEmpty()) {
            saveBatch(entities);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeTags(RemoveProductTagsDTO dto) {
        Long productId = dto.getProductId();
        List<Long> tagIds = dto.getTagIds();
        baseMapper.delete(
                Wrappers.<ProductTags>lambdaQuery()
                        .eq(ProductTags::getProductId, productId)
                        .in(ProductTags::getTagId, tagIds));
    }

    @Override
    public List<TagVO> getPopularTags(Integer limit) {
        // 查询使用频率最高的标签
        // 这里使用简单的统计查询，可以根据实际需求优化
        List<Integer> popularTagIds = baseMapper.selectPopularTagIds(limit);

        if (popularTagIds.isEmpty()) {
            return Collections.emptyList();
        }

        // 查询标签详情并转换为 VO
        List<Tags> tags = tagsMapper.selectBatchIds(popularTagIds);
        return tags.stream().map(tag -> {
            TagVO vo = new TagVO();
            BeanUtil.copyProperties(tag, vo);
            return vo;
        }).toList();
    }
}
